package com.wc.blue_bridge_test.base.T66;

import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/12/27 23:04
 * @description 回形取数
 * https://lx.lanqiao.cn/problem.page?gpid=T66
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] map = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map[i][j] = sc.nextInt();
            }
        }
        int round = (Math.min(m, n) + 1) / 2;
        StringBuffer ans = new StringBuffer();
        int idx = 0;
        int idy = 0;

        int cnt = 0;
        round = 0;
        while (true) {
            idx = round;
            idy = round;
            while (idx < n - round) {
                add(map[idx][idy], ans);
                idx++;
                cnt++;
                if (cnt == m * n) {
                    print(ans);
                    return;
                }
            }
            idx--;
            idy++;
            while (idy < m - round) {
                add(map[idx][idy], ans);
                idy++;
                cnt++;
                if (cnt == m * n) {
                    print(ans);
                    return;
                }
            }
            idy--;
            idx--;
            while (idx >= round) {
                add(map[idx][idy], ans);
                idx--;
                cnt++;
                if (cnt == m * n) {
                    print(ans);
                    return;
                }
            }
            idx++;
            idy--;
            while (idy > round) {
                add(map[idx][idy], ans);
                idy--;
                cnt++;
                if (cnt == m * n) {
                    print(ans);
                    return;
                }
            }
            round++;
        }
    }

    static void print(StringBuffer ans) {
        ans.replace(0, 1, "");
        System.out.println(ans);
    }

    static void add(int num, StringBuffer s) {
        s.append(' ');
        s.append(num);
    }
}
